﻿using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;
using RpcStore.Collect;
using RpcStore.Model.DB;
using RpcStore.Model.ServerGroup;
using RpcStore.RemoteModel.ServerBind.Model;
using RpcStore.RemoteModel.Transmit.Model;
using RpcStore.Service.Interface;

namespace RpcStore.Service.lmpl
{
    internal class ServerTransmitService : IServerTransmitService
    {
        private readonly IServerTransmitCollect _Transmit;
        private readonly IRemoteGroupCollect _Group;
        private readonly IServerRegionCollect _Region;
        private readonly IServerGroupCollect _ServerGroup;

        public ServerTransmitService (IServerTransmitCollect transmit,
            IRemoteGroupCollect group,
            IServerRegionCollect region,
            IServerGroupCollect serverGroup)
        {
            this._Transmit = transmit;
            this._Group = group;
            this._Region = region;
            this._ServerGroup = serverGroup;
        }

        public void Add (TransmitAdd add)
        {
            this._Transmit.Add(add);
        }

        public void Delete (long id)
        {
            ServerTransmitConfigModel config = this._Transmit.Get(id);
            this._Transmit.Delete(config);
        }

        public TransmitConfig[] GetTransmits (long id)
        {
            ServerTransmitConfigModel config = this._Transmit.Get(id);
            return config.TransmitConfig;
        }
        public PagingResult<TransmitServer> Query (TransmitQuery query, IBasicPage paging)
        {
            BindServerGroup[] server = this._Group.Query(query.RpcMerId, new BindQueryParam
            {
                SystemTypeId = query.SystemTypeId,
                QueryKey = query.QueryKey,
                RegionId = query.RegionId,
                ServiceState = new RpcServiceState[]
                {
                    RpcServiceState.待启用,
                    RpcServiceState.正常,
                    RpcServiceState.下线,
                }
            }, paging, out int count);
            if (server.IsNull())
            {
                return new PagingResult<TransmitServer>(count);
            }
            ServerTransmitConfigModel[] configs = this._Transmit.Gets(query.SchemeId, server.ConvertAll(c => c.ServerId));
            Dictionary<int, string> regions = this._Region.GetNames(server.Distinct(a => a.RegionId));
            Dictionary<long, string> group = this._ServerGroup.GetGroupName(server.Distinct(a => a.GroupId));
            TransmitServer[] states = server.ConvertMap<BindServerGroup, TransmitServer>((a, b) =>
            {
                b.Region = regions.GetValueOrDefault(a.RegionId);
                b.ServerGroup = group.GetValueOrDefault(a.GroupId);
                ServerTransmitConfigModel config = configs.Find(c => c.ServerId == a.ServerId);
                if (config != null)
                {
                    b.TransmitId = config.Id;
                    b.Transmits = config.TransmitConfig;
                }
                return b;
            });
            return new PagingResult<TransmitServer>(states, count);
        }
        public void Set (long id, TransmitConfig[] transmits)
        {
            ServerTransmitConfigModel config = this._Transmit.Get(id);
            this._Transmit.Set(config, transmits);
        }
    }
}
